home *** CD-ROM | disk | FTP | other *** search
/ C/C++ Users Group Library 1996 July / C-C++ Users Group Library July 1996.iso / vol_400 / 414_02 / demos / tuidemo.c < prev    next >
Encoding:
C/C++ Source or Header  |  1993-06-17  |  5.2 KB  |  217 lines

  1. /********************************* tuidemo.c ********************************/
  2. /*
  3.  * File   : tuidemo.c
  4.  * Author : P.J. Kunst  (kunst@prl.philips.nl)
  5.  * Date   : 25-02-93
  6.  * Version: 1.02
  7.  *
  8.  * Purpose: This program demonstrates the use of the 'curses' library
  9.  *          for the creation of (simple) menu-operated programs.
  10.  *          In the PD-Curses version, use is made of colors for the
  11.  *          highlighting of subwindows (title bar, status bar etc).
  12.  *          The program was tested using DJGPP 1.09 ('GO32') and
  13.  *          Turbo C (2.0), as well as on a UNIX machine (HP-UX 8.07).
  14.  *
  15.  * Acknowledgement: some ideas were borrowed from Mark Hessling's
  16.  *                  version of the 'testcurs' program.
  17.  */
  18.  
  19. #include <stdio.h>
  20. #include <stdlib.h>
  21. #include <string.h>
  22. #include "tui.h"
  23.  
  24. #ifdef PDCDEBUG
  25. char *rcsid_tuidemo = "$Header: C:\CURSES\demos\RCS\tuidemo.c 2.1 1993/06/18 20:24:14 MH Rel MH $";
  26. #endif
  27.  
  28. #define FNAME   "tui.c"    /* change this if source at other location */
  29.  
  30.  
  31. /**************************** strings entry box ***************************/
  32.  
  33. void address (void)
  34. {
  35.   char *fieldname[] = {"Name","Street","City","State","Country",(char*)0};
  36.   char *fieldbuf[5];
  37.   WINDOW *wbody = bodywin();
  38.   int i, field = 50;
  39.  
  40.   for (i=0; i<5; i++) fieldbuf[i] = (char *) calloc (field+1, sizeof(char));
  41.   if (getstrings (fieldname, fieldbuf, field) != KEY_ESC)
  42.   {
  43.     for (i=0; fieldname[i]; i++)
  44.       wprintw (wbody, "%10s : %s\n", fieldname[i], fieldbuf[i]);
  45.     wrefresh (wbody);
  46.   }
  47.   for (i=0; i<5; i++) free (fieldbuf[i]);
  48. }
  49.  
  50. /**************************** string entry box ****************************/
  51.  
  52. char *getfname (char *desc, char *fname, int field)
  53. {
  54.   char *fieldname[2] = { 0, 0 };
  55.   char *fieldbuf[1];
  56.  
  57.   fieldname[0] = desc; fieldbuf[0] = fname;
  58.   return (getstrings (fieldname, fieldbuf, field) == KEY_ESC) ? NULL : fname;
  59. }
  60.  
  61. /**************************** a very simple file browser ******************/
  62.  
  63. void showfile (char *fname)
  64. {
  65.   int i, bh = bodylen();
  66.   FILE *fp;
  67.   char buf[MAXSTRLEN];
  68.   bool ateof = FALSE;
  69.  
  70.   statusmsg ("FileBrowser: Hit key to continue, Q to quit");
  71.  
  72.   if ((fp = fopen (fname, "r")) != NULL)   /* file available ? */
  73.   {
  74.     while (!ateof)
  75.     {
  76.       clsbody ();
  77.       for (i=0; i<bh-1 && !ateof; i++)
  78.       {
  79.         buf[0] = '\0';
  80.         fgets (buf, MAXSTRLEN, fp);
  81.         if (strlen(buf)) bodymsg (buf); else ateof = TRUE;
  82.       }
  83.       switch (waitforkey())
  84.       {
  85.         case 'Q':
  86.         case 'q':
  87.         case 0x1b: /* ESCAPE */
  88.           ateof = TRUE;
  89.           break;
  90.  
  91.         default:
  92.           break;
  93.       }
  94.     }
  95.     fclose (fp);
  96.   }
  97.   else
  98.   {
  99.     sprintf (buf, "ERROR: file '%s' not found", fname);
  100.     errormsg (buf);
  101.   }
  102. }
  103.  
  104. /***************************** forward declarations ***********************/
  105.  
  106. void sub0(), sub1(), sub2(), sub3();
  107. void func1(), func2();
  108. void subfunc1(), subfunc2();
  109. void subsub();
  110.  
  111. /***************************** menus initialization ***********************/
  112.  
  113. menu MainMenu[] =
  114. {
  115.   "Asub",     sub0,     "Go inside first submenu",
  116.   "Bsub",     sub1,     "Go inside second submenu",
  117.   "Csub",     sub2,     "Go inside third submenu",
  118.   "Dsub",     sub3,     "Go inside fourth submenu",
  119.   "",         (FUNC)0,  ""    /* always add this as the last item ! */   
  120. };
  121.  
  122. menu SubMenu0[] =
  123. {
  124.   "Exit",     DoExit,     "Terminate program",
  125.   "",         (FUNC)0,  ""    /* always add this as the last item ! */   
  126. };
  127.  
  128. menu SubMenu1[] =
  129. {
  130.   "OneBeep",  func1,    "Sound one beep",
  131.   "TwoBeeps", func2,    "Sound two beeps",
  132.   "",         (FUNC)0,  ""    /* always add this as the last item ! */   
  133. };
  134.  
  135. menu SubMenu2[] =
  136. {
  137.   "Browse",   subfunc1, "Source file lister",
  138.   "Input",    subfunc2, "Interactive file lister",
  139.   "Address",  address,  "Get address data",
  140.   "",         (FUNC)0,  ""    /* always add this as the last item ! */   
  141. };
  142.  
  143. menu SubMenu3[] =
  144. {
  145.   "SubSub",   subsub,   "Go inside sub-submenu",
  146.   "",         (FUNC)0,  ""    /* always add this as the last item ! */   
  147. };
  148.  
  149. /***************************** main menu functions ************************/
  150.  
  151. void sub0 (void)
  152. {
  153.   domenu (SubMenu0);
  154. }
  155.  
  156. void sub1 (void)
  157. {
  158.   domenu (SubMenu1);
  159. }
  160.  
  161. void sub2 (void)
  162. {
  163.   domenu (SubMenu2);
  164. }
  165.  
  166. void sub3 (void)
  167. {
  168.   domenu (SubMenu3);
  169. }
  170.  
  171. /***************************** submenu1 functions *************************/
  172.  
  173. void func1 (void)
  174. {
  175.   beep ();
  176.   bodymsg ("One beep! ");
  177. }
  178.  
  179. void func2 (void)
  180. {
  181.   beep ();
  182.   bodymsg ("Two beeps! ");
  183.   beep ();
  184. }
  185.  
  186. /***************************** submenu2 functions *************************/
  187.  
  188. void subfunc1 (void)
  189. {
  190.   showfile (FNAME);
  191. }
  192.  
  193. void subfunc2 (void)
  194. {
  195.   char fname[MAXSTRLEN];
  196.  
  197.   strcpy (fname, FNAME);
  198.   if (getfname ("File to browse:", fname, 50)) showfile (fname);
  199. }
  200.  
  201. /***************************** submenu3 functions *************************/
  202.  
  203. void subsub (void)
  204. {
  205.   domenu (SubMenu2);
  206. }
  207.  
  208. /***************************** start main menu  ***************************/
  209.  
  210. int main ()
  211. {
  212.   startmenu (MainMenu, "TUI - 'textual user interface' demonstration program");
  213.  
  214.   return 0;
  215. }
  216. /********************************* tuidemo.c ********************************/
  217.